package com.java.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.mapper.MessageMapper;
import com.java.mapper.SalaryMapper;
import com.java.po.Department;
import com.java.po.Salary;
import com.java.po.SalaryMap;
import com.java.po.User;
import com.java.service.SalaryService;
import com.java.tool.AjaxJson;

/**
 * Title:SalaryServiceImpl.java
 * Description:
 * @author:sprogrammer
 * @time:2018年3月22日下午5:41:26
 */
public class SalaryServiceImpl implements SalaryService {

	private SalaryMapper salaryMapper;
	private AjaxJson ajaxJson;
	
	@SuppressWarnings("resource")
	public SalaryServiceImpl(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		salaryMapper = (SalaryMapper) applicationContext.getBean("salaryMapper");		
		ajaxJson = new AjaxJson();
	}
	
	public void cleanAjaxJson(){
		ajaxJson.setData("");
		ajaxJson.setTotal(0);
		ajaxJson.setMessage("");
	}
	

	@Override
	public AjaxJson updateSalaryById(Salary salary) throws Exception {
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		int flag = salaryMapper.updateSalaryById(salary);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	public AjaxJson insertSalary(Salary salary) throws Exception {
		// TODO 自动生成的方法存根
		cleanAjaxJson();
		int flag = salaryMapper.insertSalary(salary);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	public AjaxJson deleteSalaryById(Salary salary) throws Exception {
		// TODO 自动生成的方法存根
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		int flag = salaryMapper.deleteSalaryById(salary);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	public AjaxJson findAllSalary(SalaryMap salaryMap) throws Exception {
		// TODO 自动生成的方法存根
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		Salary salary =new Salary();
		int page = salaryMap.getPage();
		int pageSize = salaryMap.getPageSize();
		if(salaryMap.getSalary()!=null){
			salary = salaryMap.getSalary();
		}
		PageHelper.startPage(page,pageSize);
		List<Salary> list = salaryMapper.findAllSalary(salary);
		PageInfo<Salary> pageInfo = new PageInfo<>(list);
		ajaxJson.setData(pageInfo.getList());
		ajaxJson.setTotal((int)pageInfo.getTotal());
		return ajaxJson;
	}

	@Override
	public AjaxJson findSalaryByid(Salary salary) throws Exception {
		// TODO 自动生成的方法存根
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		salary =salaryMapper.findSalaryByid(salary);
		ajaxJson.setData(salary);
		ajaxJson.setTotal(1);
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

}
